package com.lanchetech.admin.service.impl;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.admin.service.AdminService;
import com.lanchetech.admin.service.VideoService;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.bean.vo.VideoVO;
import com.lanchetech.dao.SpuMapper;
import com.lanchetech.dao.VideoMapper;
import com.lanchetech.entity.Video;
import com.lanchetech.service.DaoService;
import com.lanchetech.service.OSSClientUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    VideoMapper videoMapper;

    @Autowired
    AdminService adminService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    DaoService daoService;

    @Autowired
    OSSClientUtil ossClientUtil;

    @Autowired
    SpuMapper spuMapper;

    @Override
    public ResultData<VideoVO> getVideoDetail(Long id) {
        Video video = videoMapper.selectByPrimaryKey(id);
        VideoVO vo = new VideoVO();
        BeanUtils.copyProperties(video, vo);
        if(!StringUtils.isEmpty(video.getSpuIds()) && !video.getSpuIds().equals("[]")){
            vo.setSpuList(spuMapper.findAllByIds(JSON.parseArray(video.getSpuIds(), Long.class)));
        }
        return new ResultData<>(vo);
    }

    private String buildImage(Video video){
        if(video.getMainImage() != null){
            return video.getMainImage();
        }else{
            try {
                return ossClientUtil.uploadWebImageOriginally(video.getVideo() + "?x-oss-process=video/snapshot,t_1000,f_png,w_0,h_0,m_fast");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public BaseResp editVideo(Video video) {
        if (video.getId() == null) {
            // 新增
            video.setGoods(0);
            video.setViews(0);
            video.setNickname("");
            video.setAvatar("");
            video.setMainImage(buildImage(video));
            video.setCreatedAt(new Date());
            video.setUpdatedAt(new Date());
            videoMapper.insert(video);
        } else {
            // 编辑
            video.setMainImage(buildImage(video));
            video.setUpdatedAt(new Date());
            videoMapper.updateByPrimaryKeySelective(video);
        }
        redisTemplate.delete("HomeInfoResp");
        return new BaseResp();
    }

    @Override
    public ResultData<BasePageResp<VideoVO>> getVideoPage(PageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<VideoVO> list = videoMapper.findAllPage(req);
        return PageHelpUtil.buildPage(list, page);
    }

    @Override
    public BaseResp deleteVideo(Long id) {
        videoMapper.deleteByPrimaryKey(id);
        redisTemplate.delete("HomeInfoResp");
        return new BaseResp();
    }

    @Override
    public BaseResp auditVideo(Video req) {
        videoMapper.updateStatusAndRemarkAndUpdatedAtById(req.getStatus(), req.getRemark(), new Date(), req.getId());
        return new BaseResp();
    }

    @Override
    public BaseResp updateVideoPoster(Video video) {
        videoMapper.updateMainImageById(video.getMainImage(), video.getId());
        return new BaseResp();
    }
}